package com.cblz.bd.server.utils;

import cn.dev33.satoken.stp.StpUtil;
import com.cblz.bd.common.core.constant.AdminLoginConstant;
import com.cblz.bd.common.core.constant.RedisConstant;
import com.cblz.bd.redis.spring.boot.starter.service.RedisService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.StringJoiner;

/**
 * {@code @Name:} CacheCleanUtil
 * {@code @Description: } 清除缓存工具类
 * {@code @Author:} by yanjieping
 * {@code @CreateDate:} 2022/11/21 - 周一
 */
@Component
@RequiredArgsConstructor
public class CacheCleanUtil {

    private final RedisService redisService;

    /**
     * 清除菜单缓存
     *
     * @param menuAll   所有菜单信息
     * @param menuLogin 根据登录ID清除对应的菜单信息
     * @param menuId    根据菜单ID清除对应的菜单信息
     */
    public void cleanMenuCache(boolean menuAll, boolean menuLogin, String... menuId) {
        if (menuAll)
            redisService.del(RedisConstant.MENU_ALL_PREFIX.getValue());
        if (menuLogin)
            redisService.del(RedisConstant.MENU_PREFIX.getValue() + StpUtil.getLoginId());
        if (null != menuId)
            Arrays.stream(menuId).forEach((v) -> redisService.del(RedisConstant.MENU_ID_CACHE_PREFIX.getValue() + v));
    }

    /**
     * 清除自定义Session
     */
    public void cleanCustomSession() {
        redisService.del(new StringJoiner(":")
                .add(AdminLoginConstant.SESSION.getValue())
                .add(StpUtil.getLoginId().toString())
                .add(StpUtil.getTokenValue())
                .toString());
    }
}
